﻿using System;
using System.Collections.Generic;
using System.Linq;
using YanZhiwei.DotNet3._5.Utilities.Base.SchedulerType;
using YanZhiwei.DotNet2.Utilities.Base;
using System.Collections;
namespace YanZhiwei.DotNet3._5.Utilities.Base
{
    /// <summary> 
    /// 任务管理中心 
    /// 使用它可以管理一个或则多个同时运行的任务 
    /// <para>参考：http://www.cnblogs.com/Googler/archive/2010/06/05/1752213.html </para>
    /// </summary> 
    public static class TaskScheduler
    {
        #region 构造函数，变量
        private static ThreadSafeList<Task> taskScheduler;
        /// <summary>
        /// 任务数量
        /// </summary>
        public static int Count
        {
            get { return taskScheduler.Count; }
        }
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static TaskScheduler()
        {
            taskScheduler = new ThreadSafeList<Task>();
        }
        #endregion
        #region 查找任务
        /// <summary>
        /// 查找任务
        /// </summary>
        /// <param name="name">任务名称</param>
        /// <returns>Task</returns>
        public static Task Find(string name)
        {
            return taskScheduler.Find(task => task.Name == name);
        }
        #endregion
        #region 根据任务组来查找隶属于的任务
        /// <summary>
        /// 根据任务组来查找隶属于的任务
        /// </summary>
        /// <param name="groupName">任务组名称</param>
        /// <returns>任务集合</returns>
        public static List<Task> FindByGroupName(string groupName)
        {
            return taskScheduler.FindAll(task => task.GroupName == groupName);
        }
        #endregion
        #region 组任务还有多少时间执行完
        /// <summary>
        /// 组任务还有多少时间执行完
        /// </summary>
        /// <param name="groupName">组任务名称</param>
        /// <returns>TimeSpan</returns>
        public static TimeSpan GroupTaskDueTime(string groupName)
        {
            TimeSpan _dueTime = new TimeSpan(0, 0, 0);
            List<Task> _taskList = FindByGroupName(groupName);
            if (_taskList != null && _taskList.Count > 0)
            {
                _dueTime = _taskList.Max(t => t.Shedule.ExecutionTime) - DateTime.Now;
            }
            return _dueTime;
        }
        #endregion
        #region GetEnumerator
        /// <summary>
        /// GetEnumerator
        /// </summary>
        /// <returns>IEnumerator</returns>
        public static IEnumerator GetEnumerator()
        {
            return taskScheduler.GetEnumerator();

        }
        #endregion
        #region 终止任务
        /// <summary> 
        /// 终止任务 
        /// </summary> 
        public static void TerminateAllTask()
        {
            lock (taskScheduler)
            {
                taskScheduler.ForEach(task => task.Close());
                taskScheduler.Clear();
                taskScheduler.TrimExcess();
            }
        }
        #endregion
        #region 注册任务
        /// <summary>
        /// 注册任务
        /// </summary>
        /// <param name="task">Task</param>
        internal static void Register(Task task)
        {
            lock (taskScheduler)
            {
                taskScheduler.Add(task);
            }
        }
        #endregion
        #region 移除任务
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="task">Task</param>
        internal static void Deregister(Task task)
        {
            lock (taskScheduler)
            {
                taskScheduler.Remove(task);
            }
        }
        #endregion
    }
}
